package Object.net.tjdz.service;

import Object.net.tjdz.model.Teacher;

import java.util.ArrayList;
import java.util.List;

public class TeacherService implements PersonService<Teacher> {
    private final List<Teacher> teacherDatabase = new ArrayList<>();

    @Override
    public boolean add(Teacher teacher) {
        if (getById(teacher.getId()) != null) {
            return false;
        }
        teacherDatabase.add(teacher);
        return true;
    }

    @Override
    public boolean delete(String id) {
        Teacher teacher = getById(id);
        if (teacher == null) {
            return false;
        }
        return teacherDatabase.remove(teacher);
    }

    @Override
    public boolean update(String id, Teacher newTeacher) {
        Teacher oldTeacher = getById(id);
        if (oldTeacher == null) {
            return false;
        }
        oldTeacher.setName(newTeacher.getName());
        oldTeacher.setDepartment(newTeacher.getDepartment());
        return true;
    }

    @Override
    public Teacher getById(String id) {
        return teacherDatabase.stream()
                .filter(teacher -> teacher.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    @Override
    public List<Teacher> getAll() {
        return new ArrayList<>(teacherDatabase);
    }
}